var map;
var marker;
var clcMapType256;  //ImageMapType thuc vat, dong vat
var tileSize = 256;
var polyline = null;
var polygon = null;
var lpath; // day toa do de ve cac polyline
var gpath; // day toa do de ve cac polygon
var myEventListenerLine; // lang nghe su kien line
var myEventListenerArea; // lang nghe su kien area
var kt; // kiem tra su kien measureDistance hay measureArea duoc click
var lng;
var lat;
var latLng;
var infowindow;
var markersArrayTV = []; // luu cac markers Thuc vat
var markersArrayDV = []; // luu cac markers Dong vat
var circlesArrayTV = []; // luu cac circles Thuc vat
var circlesArrayDV = []; // luu cac circles Dong vat
var dataTV = 0;
var dataDV = 0;
var layerDao, layerDuong;

function initialize() {
	var mapOptions = {
			zoom : 14,
			minZoom : 13,
			center : new google.maps.LatLng(15.958647, 108.49956),
			mapTypeId : google.maps.MapTypeId.ROADMAP
		};
		map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
		// khai bao lop layerDao
		layerDao = function(coord, zoom) {
			var lURL = GetTileURL("dao_layer_vitri", "", coord, zoom);
			return lURL;
		};
		// khai bao lop layerDuong
		layerDuong = function(coord, zoom) {
			var lURL = GetTileURL("duong_layer_vitri", "", coord, zoom);
			return lURL;
		};
		
		createLayers(layerDao, 0);
		createLayers(layerDuong, 1);
		// Thêm tên địa danh.
		addLabelOnMap(new google.maps.LatLng(15.933744,108.528225), 'BÃI HƯƠNG');
		addLabelOnMap(new google.maps.LatLng(15.945195,108.512815), 'BÃI LÀNG');
		addLabelOnMap(new google.maps.LatLng(15.961989,108.502687), 'BÃI ÔNG');
		addLabelOnMap(new google.maps.LatLng(15.946619,108.482563), 'hòn Dài');
		addLabelOnMap(new google.maps.LatLng(15.932279,108.47367), 'hòn Mồ');
		addLabelOnMap(new google.maps.LatLng(15.972304,108.447234), 'hòn Lá');
		addLabelOnMap(new google.maps.LatLng(15.974171,108.420595), 'hòn Khô Lớn');
		addLabelOnMap(new google.maps.LatLng(15.972572,108.417224), 'hòn Khô Nhỏ');
		addLabelOnMap(new google.maps.LatLng(15.90727,108.536584), 'hòn Tai');
		
		// lay cac diem hien thi len map 
		ajaxGetPoint("thucvat");
		ajaxGetPoint("dongvat");
		
}

//create layers
function createLayers(layer, at){
	clcMapType256 = new google.maps.ImageMapType(
			{
				getTileUrl: layer, 
				isPng: true,
				tileSize: new google.maps.Size(256, 256)
			}
	);
	map.overlayMapTypes.setAt(at, clcMapType256);
}

//creates the basic tile URL for WMS GetMap request for the given style, layer
//and coordinates
function GetTileURL(layerName, styleName, coord, zoom) {
	var lBbox = GetTileBoundingBox(coord, zoom);

	var lSRS = "EPSG:4326";
	var lURL = "http://localhost:8088/geoserver/wms?";
	lURL += "&REQUEST=GetMap";
	lURL += "&SERVICE=WMS";
	lURL += "&VERSION=1.1.1";
	lURL += "&LAYERS=" + layerName;
	lURL += "&STYLES=" + styleName;
	lURL += "&FORMAT=image/png";
	lURL += "&BGCOLOR=0xFFFFFF";
	lURL += "&TRANSPARENT=TRUE";
	lURL += "&SRS=EPSG:4326";
	lURL += "&BBOX=" + lBbox;
	lURL += "&WIDTH=" + tileSize;
	lURL += "&HEIGHT=" + tileSize;
	lURL += "&reaspect=false";
	lURL += "&buffer=50";
	return lURL;
}

function GetTileBoundingBox(coord, zoom) {
	// map projection for coordinate transformations
	var projection = map.getProjection();

	var zfactor = Math.pow(2, zoom);

	// getting corners points
	var lULP = new google.maps.Point(coord.x * tileSize / zfactor,
			(coord.y + 1) * tileSize / zfactor);
	var lLRP = new google.maps.Point((coord.x + 1) * tileSize / zfactor,
			coord.y * tileSize / zfactor);

	// getting corner points lat/lng coordinates
	var lUL = projection.fromPointToLatLng(lULP);
	var lLR = projection.fromPointToLatLng(lLRP);

	// getting string bounding box
	var lBbox = lUL.lng() + "," + lUL.lat() + "," + lLR.lng() + "," + lLR.lat();
	return lBbox;
}

function addLabelOnMap(latLongLabel, name){
	markerLabel = new MarkerWithLabel({
	       position: latLongLabel,
	       map: map,
	       labelContent: name,
	       labelAnchor: new google.maps.Point(0, 0),
	       labelClass: "labels", // the CSS class for the label
	       labelStyle: {opacity: 1},
	       icon: {}
	     });
}
// -----------------------------------------begin cac ham xu ly ban do----------------------------------------------------
// change map type
function mapView(){
	if(document.getElementById("mapselect").options[0].selected == true){
		map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
		createLayers(layerDao, 0);
		createLayers(layerDuong, 1);
	}
	else if(document.getElementById("mapselect").options[1].selected == true){
		map.setMapTypeId(google.maps.MapTypeId.HYBRID);
		map.overlayMapTypes.setAt(0, null);
		map.overlayMapTypes.setAt(1, null);
	}
	else if(document.getElementById("mapselect").options[2].selected == true){
		map.setMapTypeId(google.maps.MapTypeId.TERRAIN);
		map.overlayMapTypes.setAt(0, null);
		map.overlayMapTypes.setAt(1, null);
	}
}

//measure distance
function measureDistance(){
	kt = 0;
	document.getElementById('distance').innerHTML = "km";
	google.maps.event.removeListener(myEventListenerArea);
	var lineOptions = {
			strokeColor: '#FF0000',
			strokeOpacity: 0.8,
			strokeWeight: 2
			}
		polyline = new google.maps.Polyline(lineOptions);
		polyline.setMap(map);
		// add a listener for the click event
		myEventListenerLine = google.maps.event.addListener(map, 'click', addLatLngLine);
}

function addLatLngLine(event) {
	lpath = polyline.getPath();
	lpath.push(event.latLng);
	document.getElementById('measure').value = google.maps.geometry.spherical.computeLength(lpath.getArray()) / 1000;
}

// measure area
function measureArea() {
	kt = 1;
	document.getElementById('distance').innerHTML = "m2";
	google.maps.event.removeListener(myEventListenerLine);
	 var gonOptions = {
             fillColor: '#0000FF',
             fillOpacity: 0.4,
             strokeWeight: 0
           };
         polygon = new google.maps.Polygon(gonOptions);
         polygon.setMap(map);
         myEventListenerArea = google.maps.event.addListener(map, 'click', addLatLngArea);
}

function addLatLngArea(event) {
	gpath = polygon.getPath();
	gpath.push(event.latLng);

	if (gpath.getLength() > 2) {
		var garray = gpath.getArray();
		var area = google.maps.geometry.spherical.computeArea(garray);
		document.getElementById('measure').value = area;
	}
}

function destroyEvent(){
	google.maps.event.removeListener(myEventListenerLine);
	google.maps.event.removeListener(myEventListenerArea);
}

function undoLine() {
    if (kt == 0 && lpath) {
         lpath.pop();
		   document.getElementById('measure').value = google.maps.geometry.spherical.computeLength(lpath.getArray());
		}
    if(kt == 1 && gpath){
    	gpath.pop();
		   document.getElementById('measure').value = google.maps.geometry.spherical.computeArea(gpath.getArray());
    }
    if(kt == 2 && gpath) gpath.pop();
  }

function refresh(){
	if(polygon)
		polygon.setMap(null);  // lpath.length = 0;
	if(polyline)
		polyline.setMap(null); // gpath.length = 0;
	
	destroyEvent();
	
}

function printpage()
{
	window.print();
}
// ---------------------------------------- end cac ham xu ly ban do------------------------------------------------------


function ajaxGetPoint(name){
	$.ajax({
		type : "POST",
		url : "/CuLaoCham/getData.do",
		data: {name: name},
		cache : false,
		dataType : "json",
		success : function(data) {
			if(name == "thucvat") dataTV = data;
			else if(name == "dongvat") dataDV = data;
	},
	error : function(e) {
		alert('Error ' + e);
	}
	}); 
}

var imagetv = 'image/thucvat/poi_1.svg';
var imagedv = 'image/dongvat/poi_3.svg';
var latitude;
var longitude;
// hien thi cac loai thuc vat
var checkActionTV=0;
function displayPoinThucVat(nametv, loaiTV){ 
	var checboxChecked = document.getElementById(nametv).checked;
	checkActionTV++;
	if(checboxChecked){
		if(nametv == "checkThucVat"){
			ajaxGetPoint("thucvat");
			deleteAllMarkerTV();
			deleteAllCircleTV();
			displayAllMarkerTV();
		} else getLatLongPointTV(dataTV, nametv, loaiTV);
	} else {
		//removeDisplayLayer(false, 0);
		if(nametv == "checkThucVat") {
			deleteAllMarkerTV();
			deleteAllCircleTV();
		}
		else {
			if(checkActionTV == 1) displayAllMarkerTV();
			deleteMarkerTV(nametv);
		}
	}
}
// hien thi cac loai dong vat
var checkActionDV=0;
function displayPoinDongVat(namedv, loaiDV){
	var checboxChecked = document.getElementById(namedv).checked;
	checkActionDV++;
	if(checboxChecked){
		if(namedv == "checkDongVat"){
			ajaxGetPoint("dongvat");
			deleteAllMarkerDV();
			deleteAllCircleDV();
			displayAllMarkerDV();
		} else getLatLongPointDV(dataDV, namedv, loaiDV);
	} else {
		//removeDisplayLayer(false, 0);
		if(namedv == "checkDongVat") {
			deleteAllMarkerDV();
			deleteAllCircleDV();
		}
		else {
			if(checkActionDV == 1) displayAllMarkerDV();
			deleteMarkerDV(namedv);
		}
	}
}


// Hiễn thị tất cả các loài thực vật
function displayAllMarkerTV() {
	var nameTV = "";
	// Hiển thị các loài thực vật Lâm sản
	for ( var i = 1; i <= dataTV.pointTVLS.length; i++) {
		latitude = dataTV.pointTVLS[i][2];
		longitude = dataTV.pointTVLS[i][3];
		banKinh = dataTV.pointTVLS[i][4];
		matDo = dataTV.pointTVLS[i][5];
		for ( var j = 1; j <= dataTV.infoTVLS.length; j++){
			if(dataTV.pointTVLS[i][1] == dataTV.infoTVLS[j][0]) { // kiem tra de lay ma TVLS
				for(var k = 1; k <= dataTV.infoTVHST.length; k++){ 
					if(dataTV.infoTVLS[j][1] == dataTV.infoTVHST[k][0]){ // kiem tra de lay ten TVLS giong voi ma TVLS o tren
						nameTV = dataTV.infoTVHST[k][2];
						break;
					}
				}
				break;
			}
		}
		//addMarkerTV(latitude, longitude, nameTV);
		addMarkerWithLabelTV(latitude, longitude,"tvLamSan", nameTV, matDo);
		if(banKinh != 0) addCircleTV(latitude, longitude, banKinh);
	}
	// Hiển thị các loài thực vật Lâm sản ngoài gỗ
	for ( var i = 1; i <= dataTV.pointTVLSNG.length; i++) {
		latitude = dataTV.pointTVLSNG[i][2];
		longitude = dataTV.pointTVLSNG[i][3];
		banKinh = dataTV.pointTVLSNG[i][4];
		matDo = dataTV.pointTVLSNG[i][5];
		for ( var j = 1; j <= dataTV.infoTVLSNG.length; j++){
			if(dataTV.pointTVLSNG[i][1] == dataTV.infoTVLSNG[j][0]) {
				for(var k = 1; k <= dataTV.infoTVHST.length; k++){ 
					if(dataTV.infoTVLSNG[j][1] == dataTV.infoTVHST[k][0]){
						nameTV = dataTV.infoTVHST[k][2];
						break;
					}
				}
				break;
			}
		}
		//addMarkerTV(latitude, longitude, nameTV);
		addMarkerWithLabelTV(latitude, longitude,"tvLamSanNgoaiGo", nameTV, matDo);
		if(banKinh != 0) addCircleTV(latitude, longitude, banKinh);
	}
	// Hiển thị các loài thực vật San hô
	for ( var i = 1; i <= dataTV.pointTVSH.length; i++) {
		latitude = dataTV.pointTVSH[i][2];
		longitude = dataTV.pointTVSH[i][3];
		banKinh = dataTV.pointTVSH[i][4];
		matDo = dataTV.pointTVSH[i][5];
		for ( var j = 1; j <= dataTV.infoTVSH.length; j++){
			if(dataTV.pointTVSH[i][1] == dataTV.infoTVSH[j][0]) {
				for(var k = 1; k <= dataTV.infoTVHST.length; k++){ 
					if(dataTV.infoTVSH[j][1] == dataTV.infoTVHST[k][0]){
						nameTV = dataTV.infoTVHST[k][2];
						break;
					}
				}
				break;
			}
		}
		//addMarkerTV(latitude, longitude, nameTV);
		addMarkerWithLabelTV(latitude, longitude,"tvSanHo", nameTV, matDo);
		if(banKinh != 0) addCircleTV(latitude, longitude, banKinh);
	}
	// Hiển thị các loài thực vật Thảm cỏ biển
	for ( var i = 1; i <= dataTV.pointTVTCB.length; i++) {
		latitude = dataTV.pointTVTCB[i][2];
		longitude = dataTV.pointTVTCB[i][3];
		banKinh = dataTV.pointTVTCB[i][4];
		matDo = dataTV.pointTVTCB[i][5];
		for ( var j = 1; j <= dataTV.infoTVTCB.length; j++){
			if(dataTV.pointTVTCB[i][1] == dataTV.infoTVTCB[j][0]) {
				for(var k = 1; k <= dataTV.infoTVHST.length; k++){ 
					if(dataTV.infoTVTCB[j][1] == dataTV.infoTVHST[k][0]){
						nameTV = dataTV.infoTVHST[k][2];
						break;
					}
				}
				break;
			}
		}
		//addMarkerTV(latitude, longitude, nameTV);
		addMarkerWithLabelTV(latitude, longitude,"tvThamCoBien", nameTV, matDo);
		if(banKinh != 0) addCircleTV(latitude, longitude, banKinh);
	}
	// Hiển thị các loài thực vật Trồng trọt
	for ( var i = 1; i <= dataTV.pointTVTT.length; i++) {
		latitude = dataTV.pointTVTT[i][2];
		longitude = dataTV.pointTVTT[i][3];
		banKinh = dataTV.pointTVTT[i][4];
		matDo = dataTV.pointTVTT[i][5];
		for ( var j = 1; j <= dataTV.infoTVTT.length; j++){
			if(dataTV.pointTVTT[i][1] == dataTV.infoTVTT[j][0]) {
				for(var k = 1; k <= dataTV.infoTVHST.length; k++){ 
					if(dataTV.infoTVTT[j][1] == dataTV.infoTVHST[k][0]){
						nameTV = dataTV.infoTVHST[k][2];
						break;
					}
				}
				break;
			}
		}
		//addMarkerTV(latitude, longitude, nameTV);
		addMarkerWithLabelTV(latitude, longitude,"tvTrongTrot", nameTV, matDo);
		if(banKinh != 0) addCircleTV(latitude, longitude, banKinh);
	}
}

//Hiễn thị tất cả các loài động vật
function displayAllMarkerDV() {
	var nameDV = "";
	// Hiển thị các loài động vật Lâm sản ngoài gỗ
	for ( var i = 1; i <= dataDV.pointDVLSNG.length; i++) {
		latitude = dataDV.pointDVLSNG[i][2];
		longitude = dataDV.pointDVLSNG[i][3];
		banKinh = dataDV.pointDVLSNG[i][4];
		matDo = dataDV.pointDVLSNG[i][5];
		for ( var j = 1; j <= dataDV.infoDVLSNG.length; j++){
			if(dataDV.pointDVLSNG[i][1] == dataDV.infoDVLSNG[j][0]) {
				for(var k = 1; k <= dataDV.infoDVHST.length; k++){ 
					if(dataDV.infoDVLSNG[j][1] == dataDV.infoDVHST[k][0]){
						nameDV = dataDV.infoDVHST[k][6];
						break;
					}
				}
				break;
			}
		}		
		addMarkerWithLabelDV(latitude, longitude,"dvLamSanNgoaiGo", nameDV, matDo);
		if(banKinh != 0) addCircleDV(latitude, longitude, banKinh);
	}
	// Hiển thị các loài động vật San hô
	for ( var i = 1; i <= dataDV.pointDVSH.length; i++) {
		latitude = dataDV.pointDVSH[i][2];
		longitude = dataDV.pointDVSH[i][3];
		banKinh = dataDV.pointDVSH[i][4];
		matDo = dataDV.pointDVSH[i][5];
		for ( var j = 1; j <= dataDV.infoDVSH.length; j++){
			if(dataDV.pointDVSH[i][1] == dataDV.infoDVSH[j][0]) {
				for(var k = 1; k <= dataDV.infoDVHST.length; k++){ 
					if(dataDV.infoDVSH[j][1] == dataDV.infoDVHST[k][0]){
						nameDV = dataDV.infoDVHST[k][6];
						break;
					}
				}
				break;
			}
		}
		addMarkerWithLabelDV(latitude, longitude,"dvSanHo", nameDV, matDo);
		if(banKinh != 0) addCircleDV(latitude, longitude, banKinh);
	}
	// Hiển thị các loài động vật Thảm cỏ biển
	for ( var i = 1; i <= dataDV.pointDVTCB.length; i++) {
		latitude = dataDV.pointDVTCB[i][2];
		longitude = dataDV.pointDVTCB[i][3];
		banKinh = dataDV.pointDVTCB[i][4];
		matDo = dataDV.pointDVTCB[i][5];
		for ( var j = 1; j <= dataDV.infoDVTCB.length; j++){
			if(dataDV.pointDVTCB[i][1] == dataDV.infoDVTCB[j][0]) {
				for(var k = 1; k <= dataDV.infoDVHST.length; k++){ 
					if(dataDV.infoDVTCB[j][1] == dataDV.infoDVHST[k][0]){
						nameDV = dataDV.infoDVHST[k][6];
						break;
					}
				}
				break;
			}
		}
		addMarkerWithLabelDV(latitude, longitude, "dvThamCoBien", nameDV, matDo);
		if(banKinh != 0) addCircleDV(latitude, longitude, banKinh);
	}
	// Hiển thị các loài động vật Chăn nuôi
	for ( var i = 1; i <= dataDV.pointDVCN.length; i++) {
		latitude = dataDV.pointDVCN[i][2];
		longitude = dataDV.pointDVCN[i][3];
		banKinh = dataDV.pointDVCN[i][4];
		matDo = dataDV.pointDVCN[i][5];
		for ( var j = 1; j <= dataDV.infoDVCN.length; j++){
			if(dataDV.pointDVCN[i][1] == dataDV.infoDVCN[j][0]) {
				for(var k = 1; k <= dataDV.infoDVHST.length; k++){ 
					if(dataDV.infoDVCN[j][1] == dataDV.infoDVHST[k][0]){
						nameDV = dataDV.infoDVHST[k][2];
						break;
					}
				}
				break;
			}
		}
		addMarkerWithLabelDV(latitude, longitude, "dvChanNuoi", nameDV, matDo);
		if(banKinh != 0) addCircleDV(latitude, longitude, banKinh);
	}
}

function deleteAllMarkerTV(){
	if (markersArrayTV) {
	    for (i in markersArrayTV) {
	    	markersArrayTV[i].setMap(null);
	    }
	    markersArrayTV.length = 0;
	  }
}

function deleteAllCircleTV(){
	if (circlesArrayTV) {
	    for (i in circlesArrayTV) {
	    	circlesArrayTV[i].setMap(null);
	    }
	    circlesArrayTV.length = 0;
	  }
}

function deleteAllMarkerDV(){
	if (markersArrayDV) {
	    for (i in markersArrayDV) {
	    	markersArrayDV[i].setMap(null);
	    }
	    markersArrayDV.length = 0;
	  }
}

function deleteAllCircleDV(){
	if (circlesArrayDV) {
	    for (i in circlesArrayDV) {
	    	circlesArrayDV[i].setMap(null);
	    }
	    circlesArrayDV.length = 0;
	  }
}

// tao marker thuc vat
function addMarkerWithLabelTV(latatude, longatude, loaitv, name, matdo){
	var markerWithLabel;
	if(matdo != ""){
		   markerWithLabel = new MarkerWithLabel({
	       position: new google.maps.LatLng(latatude,longatude),
	       map: map,
	       labelContent: matdo,
	       labelAnchor: new google.maps.Point(20, 0),
	       labelClass: "labels", // the CSS class for the label
	       title: name,
	       labelStyle: {opacity: 0.75},
	       icon: {}
	     });
	} else { 
		   markerWithLabel = new MarkerWithLabel({
	       position: new google.maps.LatLng(latatude,longatude),
	       map: map,
	       labelContent: matdo,
	       labelAnchor: new google.maps.Point(10, 0),
	       labelClass: "labels", // the CSS class for the label
	       title: name,
	       labelStyle: {opacity: 0.75},
	       icon: imagetv
	     });
	}
	google.maps.event.addListener(markerWithLabel, 'click', function(e) {
		showMapInfoBubbleMessage(markerWithLabel, loaitv, name, latatude, longatude);
	});
	markersArrayTV.push(markerWithLabel);
}

//tao marker dong vat
function addMarkerWithLabelDV(latatude, longatude, loaidv, name, matdo){
	var markerWithLabel;
	if(matdo != ""){
		   markerWithLabel = new MarkerWithLabel({
	       position: new google.maps.LatLng(latatude,longatude),
	       map: map,
	       labelContent: matdo,
	       labelAnchor: new google.maps.Point(20, 0),
	       labelClass: "labels", // the CSS class for the label
	       title: name,
	       labelStyle: {opacity: 0.75},
	       icon: {}
	     });
	} else { 
		   markerWithLabel = new MarkerWithLabel({
	       position: new google.maps.LatLng(latatude,longatude),
	       map: map,
	       labelContent: matdo,
	       labelAnchor: new google.maps.Point(10, 0),
	       labelClass: "labels", // the CSS class for the label
	       title: name,
	       labelStyle: {opacity: 0.75},
	       icon: imagedv
	     });
	}
	google.maps.event.addListener(markerWithLabel, 'click', function(e) {
		showMapInfoBubbleMessage(markerWithLabel, loaidv, name, latatude, longatude);
	});
	markersArrayDV.push(markerWithLabel);
}

//tạo vùng khảo sát của thực vật
function addCircleTV(latitude, longitude, bankinh) {
	var circle = new google.maps.Circle({
        center: new google.maps.LatLng(latitude,longitude),
        map: map,
		radius: bankinh,
        fillColor: '#00FF06',
        fillOpacity: 0.5,
        strokeColor: '#00FF06',
        strokeOpacity: 0.7,
        strokeWeight: 2
    });
    map.fitBounds(circle.getBounds());
    circlesArrayTV.push(circle);
}

// tạo vùng khảo sát của động vật
function addCircleDV(latitude, longitude, bankinh) {
	var circle = new google.maps.Circle({
        center: new google.maps.LatLng(latitude,longitude),
        map: map,
		radius: bankinh,
        fillColor: '#FFDCA8',
        fillOpacity: 0.8,
        strokeColor: '#FFDCA8',
        strokeOpacity: 1,
        strokeWeight: 2
    });
    map.fitBounds(circle.getBounds());
    circlesArrayDV.push(circle);
}
/*
function getCenterPopulation(latatude, longatude){
	pointMap = {
			  center: new google.maps.LatLng(latatude, longatude),
			  population: 518
			};
	return pointMap;
}*/

/*function removeDisplayLayer(kt, layer){
	if(!kt && layer == 0) map.overlayMapTypes.setAt(0, null);
	else
		if(!kt && layer == 1) map.overlayMapTypes.setAt(1, null);

}*/
// Hiễn thị các loài thực vật đã chọn
function getLatLongPointTV(data, name, loaiTV){
	// Hiển thi thực vật lâm sản
	if(loaiTV == "thucVatLSG"){
		for( var i = 1; i <= dataTV.infoTVHST.length; i++){
			if(data.infoTVHST[i][2] == name){
				for(var j = 1; j <= dataTV.infoTVLS.length; j++) {
					if(data.infoTVHST[i][0] == data.infoTVLS[j][1]){
						for(var k = 1; k <= dataTV.pointTVLS.length; k++){
							if(data.infoTVLS[j][0] == dataTV.pointTVLS[k][1]) {
								latitude = data.pointTVLS[k][2];
								longitude = data.pointTVLS[k][3];
								banKinh = data.pointTVLS[k][4];
								matDo = data.pointTVLS[k][5];
								addMarkerWithLabelTV(latitude, longitude, data.infoTVHST[i][2], matDo);
								if(banKinh != 0) addCircleTV(latitude, longitude, banKinh);
							}
						}
					}
				}
				break;
			}
		}
	}
	// Hiển thi thực vật lâm sản ngoài gỗ
	if(loaiTV == "thucVatLSNG"){
		for( var i = 1; i <= dataTV.infoTVHST.length; i++){
			if(data.infoTVHST[i][2] == name){
				for(var j = 1; j <= dataTV.infoTVLSNG.length; j++) {
					if(data.infoTVHST[i][0] == data.infoTVLSNG[j][1]){
						for(var k = 1; k <= dataTV.pointTVLSNG.length; k++){
							if(data.infoTVLSNG[j][0] == dataTV.pointTVLSNG[k][1]) {
								latitude = data.pointTVLSNG[k][2];
								longitude = data.pointTVLSNG[k][3];
								banKinh = data.pointTVLSNG[k][4];
								matDo = data.pointTVLSNG[k][5];
								addMarkerWithLabelTV(latitude, longitude, data.infoTVHST[i][2], matDo);
								if(banKinh != 0) addCircleTV(latitude, longitude, banKinh);
							}
						}
					}
				}
				break;
			}
		}
	}
	// Hiển thi thực vật trồng trọt
	if(loaiTV == "thucVatTT"){
		for( var i = 1; i <= dataTV.infoTVHST.length; i++){
			if(data.infoTVHST[i][2] == name){
				for(var j = 1; j <= dataTV.infoTVTT.length; j++) {
					if(data.infoTVHST[i][0] == data.infoTVTT[j][1]){
						for(var k = 1; k <= dataTV.pointTVTT.length; k++){
							if(data.infoTVTT[j][0] == dataTV.pointTVTT[k][1]) {
								latitude = data.pointTVTT[k][2];
								longitude = data.pointTVTT[k][3];
								banKinh = data.pointTVTT[k][4];
								matDo = data.pointTVTT[k][5];
								addMarkerWithLabelTV(latitude, longitude, data.infoTVHST[i][2], matDo);
								if(banKinh != 0) addCircleTV(latitude, longitude, banKinh);
							}
						}
					}
				}
				break;
			}
		}
	}
	// Hiển thi thực vật thảm cỏ biển
	if(loaiTV == "thucVatTCB"){
		for( var i = 1; i <= dataTV.infoTVHST.length; i++){
			if(data.infoTVHST[i][2] == name){
				for(var j = 1; j <= dataTV.infoTVTCB.length; j++) {
					if(data.infoTVHST[i][0] == data.infoTVTCB[j][1]){
						for(var k = 1; k <= dataTV.pointTVTCB.length; k++){
							if(data.infoTVTCB[j][0] == dataTV.pointTVTCB[k][1]) {
								latitude = data.pointTVTCB[k][2];
								longitude = data.pointTVTCB[k][3];
								banKinh = data.pointTVTCB[k][4];
								matDo = data.pointTVTCB[k][5];
								addMarkerWithLabelTV(latitude, longitude, data.infoTVHST[i][2], matDo);
								if(banKinh != 0) addCircleTV(latitude, longitude, banKinh);
							}
						}
					}
				}
				break;
			}
		}
	}
	// Hiển thi thực vật san hô
	if(loaiTV == "thucVatSH"){
		for( var i = 1; i <= dataTV.infoTVHST.length; i++){
			if(data.infoTVHST[i][2] == name){
				for(var j = 1; j <= dataTV.infoTVSH.length; j++) {
					if(data.infoTVHST[i][0] == data.infoTVSH[j][1]){
						for(var k = 1; k <= dataTV.pointTVSH.length; k++){
							if(data.infoTVSH[j][0] == dataTV.pointTVSH[k][1]) {
								latitude = data.pointTVSH[k][2];
								longitude = data.pointTVSH[k][3];
								banKinh = data.pointTVSH[k][4];
								matDo = data.pointTVSH[k][5];
								addMarkerWithLabelTV(latitude, longitude, data.infoTVHST[i][2], matDo);
								if(banKinh != 0) addCircleTV(latitude, longitude, banKinh);
							}
						}
					}
				}
				break;
			}
		}
	}
	
}

//Hiễn thị các loài động vật đã chọn
function getLatLongPointDV(data, name, loaiDV){
	// Hiển thi động vật lâm sản ngoài gỗ
	if(loaiDV == "dongVatLSNG"){
		for( var i = 1; i <= dataDV.infoDVHST.length; i++){
			if(data.infoDVHST[i][6] == name){
				for(var j = 1; j <= dataDV.infoDVLSNG.length; j++) {
					if(data.infoDVHST[i][0] == data.infoDVLSNG[j][1]){
						for(var k = 1; k <= dataDV.pointDVLSNG.length; k++){
							if(data.infoDVLSNG[j][0] == dataDV.pointDVLSNG[k][1]) {
								latitude = data.pointDVLSNG[k][2];
								longitude = data.pointDVLSNG[k][3];
								banKinh = data.pointDVLSNG[k][4];
								matDo = data.pointDVLSNG[k][5];
								addMarkerWithLabelDV(latitude, longitude, data.infoDVHST[i][6], matDo);
								if(banKinh != 0) addCircleDV(latitude, longitude, banKinh);
							}
						}
					}
				}
				break;
			}
		}
	}
	// Hiển thi động vật chăn nuôi
	if(loaiDV == "dongVatCN"){
		for( var i = 1; i <= dataDV.infoDVHST.length; i++){
			if(data.infoDVHST[i][6] == name){
				for(var j = 1; j <= dataDV.infoDVCN.length; j++) {
					if(data.infoDVHST[i][0] == data.infoDVTCN[j][1]){
						for(var k = 1; k <= dataDV.pointDVCN.length; k++){
							if(data.infoDVCN[j][0] == dataDV.pointDVCN[k][1]) {
								latitude = data.pointDVCN[k][2];
								longitude = data.pointDVCN[k][3];
								banKinh = data.pointDVCN[k][4];
								matDo = data.pointDVCN[k][5];
								addMarkerWithLabelDV(latitude, longitude, data.infoDVHST[i][6], matDo);
								if(banKinh != 0) addCircleDV(latitude, longitude, banKinh);
							}
						}
					}
				}
				break;
			}
		}
	}
	// Hiển thi động vật thảm cỏ biển
	if(loaiDV == "dongVatTCB"){
		for( var i = 1; i <= dataDV.infoDVHST.length; i++){
			if(data.infoDVHST[i][6] == name){
				for(var j = 1; j <= dataDV.infoDVTCB.length; j++) {
					if(data.infoDVHST[i][0] == data.infoDVTCB[j][1]){
						for(var k = 1; k <= dataDV.pointDVTCB.length; k++){
							if(data.infoDVTCB[j][0] == dataDV.pointDVTCB[k][1]) {
								latitude = data.pointDVTCB[k][2];
								longitude = data.pointDVTCB[k][3];
								banKinh = data.pointDVTCB[k][4];
								matDo = data.pointDVTCB[k][5];
								addMarkerWithLabelDV(latitude, longitude, data.infoDVHST[i][6], matDo);
								if(banKinh != 0) addCircleDV(latitude, longitude, banKinh);
							}
						}
					}
				}
				break;
			}
		}
	}
	// Hiển thi động vật san hô
	if(loaiDV == "dongVatSH"){
		for( var i = 1; i <= dataDV.infoDVHST.length; i++){
			if(data.infoDVHST[i][6] == name){
				for(var j = 1; j <= dataDV.infoDVSH.length; j++) {
					if(data.infoDVHST[i][0] == data.infoDVSH[j][1]){
						for(var k = 1; k <= dataDV.pointDVSH.length; k++){
							if(data.infoDVSH[j][0] == dataDV.pointDVSH[k][1]) {
								latitude = data.pointDVSH[k][2];
								longitude = data.pointDVSH[k][3];
								banKinh = data.pointDVSH[k][4];
								matDo = data.pointDVSH[k][5];
								addMarkerWithLabelDV(latitude, longitude, data.infoDVHST[i][6], matDo);
								if(banKinh != 0) addCircleDV(latitude, longitude, banKinh);
							}
						}
					}
				}
				break;
			}
		}
	}
	
}

google.maps.event.addDomListener(window, 'load', initialize);




















